﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GEPSCodeTemplate
{
    public partial class ParamReplaceForm : Form
    {
        private Parser _parser;

        public string Result 
        {
            get { return _parser.Result; }
        }

        private ParamReplaceForm()
        {
            InitializeComponent();
        }

        public ParamReplaceForm(string content)
            : this()
        {
            _parser = new Parser(content);
            txtOrigin.Text = _parser.Content;
            txtResult.Text = _parser.Result;
            InitParams();
        }

        private void InitParams()
        {
            foreach (string key in _parser.Params.Keys)
            {
                AddParam(_parser.Params[key]);
            }
        }

        private void AddParam(Param param)
        {
            ParamDisplayFrame oFrame = new ParamDisplayFrame(param);
            oFrame.Parent = groupParams;
            oFrame.Dock = DockStyle.Top;
            oFrame.TextChanged = ParamChanged;
        }

        private void ParamChanged(ParamDisplayFrame frame, Param param)
        {
            _parser.UpdateParam(param.Name, param.Value);
            _parser.ReplaceAllParams();
            txtResult.Text = _parser.Result;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            Clipboard.SetText(_parser.Result);
            DialogResult = DialogResult.OK;
        }

        public static string Execute(string content)
        {

            ParamReplaceForm oForm = new ParamReplaceForm(content);
            oForm.ShowDialog();
            return oForm.Result;
        }
    }
}
